﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GUIServerConnectorAPI;
using System.Data.Entity;
using GUI;
using EntityModel;

namespace LoginForm
{
    public partial class IngrijitorForm : Form
    {
        private LoginForm loginForm;
        private Guid sessionId;
        private ListViewHitTestInfo hitinfo;
        private TextBox editbox = new TextBox();

        public IngrijitorForm(LoginForm loginForm)
        {
            InitializeComponent();
            this.loginForm = loginForm;
        }
        #region DataEdit in ListView
        public IngrijitorForm()
        {
            InitializeComponent();
            editbox.Parent = listView2;
            editbox.Hide();
            editbox.LostFocus += new EventHandler(editbox_LostFocus);
            listView2.MouseDoubleClick += new MouseEventHandler(listView2_MouseDoubleClick);
            listView2.FullRowSelect = true;
            listView2.LabelEdit = false;
        }
        private void listView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            hitinfo = listView2.HitTest(e.X, e.Y);
            ListViewItem item = hitinfo.Item;
            if (((item.SubItems.IndexOf(hitinfo.SubItem).ToString()) == "1") || ((item.SubItems.IndexOf(hitinfo.SubItem).ToString()) == "2"))
            {
                editbox.Bounds = hitinfo.SubItem.Bounds;
                editbox.Text = hitinfo.SubItem.Text;
                editbox.Show();
                editbox.Focus();
            }
            else
                listView2.LabelEdit = false;

        }
        void editbox_LostFocus(object sender, EventArgs e)
        {
            hitinfo.SubItem.Text = editbox.Text;
            editbox.Hide();
        }
        #endregion
        public void Show(Guid sessionId)
        {
            this.sessionId = sessionId;
            Show();
        }
        private void listView2_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView2.Columns[e.ColumnIndex].Width;
        }

        private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
        }
        private void IngrijitorForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GUIServerConnector.getInstance().getAuthenticator().logout(sessionId);
            this.Hide();
            loginForm.Reset(this);
        }

        private void IngrijitorForm_Load(object sender, EventArgs e)
        {
          // TODO: Uncomment dupa implementarea metodei
          // * 
          List<Pacient> listaPacienti = GUIServerConnector.getInstance().getServerInterface().getPacientsData(sessionId);
            listView1.BeginUpdate();
            foreach (Pacient pacient in listaPacienti)
            {
                ListViewItem item = new ListViewItem(pacient.Nume.ToString() + " " + pacient.Prenume.ToString());
                listView1.Items.Add(item);
            }
            listView1.EndUpdate();
          //  */
            /*
            ListViewItem nume = new ListViewItem("Nume:");//remove all dupa implementarea metodei
            nume.SubItems.Add("");
            nume.SubItems.Add("");
            listView2.Items.Add(nume);
             * */
        }


        public void LoadPacientSenzorsData(Pacient pacient)
        {
            
            List<DefinitieSenzori> defSenzori = GUIServerConnector.getInstance().getServerInterface().getSensorDefinitions(sessionId);
            List<ValoriSensori> valSenzori = GUIServerConnector.getInstance().getServerInterface().getSensorData(sessionId);
            listView2.BeginUpdate();
            foreach (DefinitieSenzori senzor in defSenzori)
            {
                foreach (ValoriSensori valoare in valSenzori)
                {

                    if (senzor.idDefinitie.Equals(valoare.idDefinitie) && (pacient.idPacient.Equals(valoare.idPacient)))
                    {
                        int i = 0;
                        ListViewItem senzorCol = new ListViewItem("Senzor[" + i + "]:");
                        senzorCol.SubItems.Add(valoare.ValoareCitita.ToString());
                        senzorCol.SubItems.Add(senzor.Observatii.ToString());
                        listView2.Items.Add(senzorCol);
                        i++;
                    }
                }
            }
            listView2.EndUpdate();
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Pacient> listaPacienti = GUIServerConnector.getInstance().getServerInterface().getPacientsData(sessionId);

            foreach (Pacient pacient in listaPacienti)
            {
                if (listView2.SelectedItems.Count > 0)
                {
                    foreach (ListViewItem selected in listView2.SelectedItems)
                    {
                        string numePrenume = pacient.Nume + " " + pacient.Prenume;
                        if (selected.Text.Equals(numePrenume))
                        {
                            LoadPacientSenzorsData(pacient);
                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
